package com.itany.shop.service.impl;

import com.itany.shop.dao.UserDao;
import com.itany.shop.entity.User;
import com.itany.shop.exception.*;
import com.itany.shop.factory.ObjectFactory;
import com.itany.shop.service.UserService;
import com.itany.shop.util.MD5Util;
import com.itany.shop.util.ParameterUtil;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import sun.security.provider.MD5;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.List;

/**
 * @author 石小俊
 * @date 2023年07月05日 16:55
 */
public class UserServiceImpl implements UserService {

    private UserDao userDao = (UserDao) ObjectFactory.getObject("userDao");

    @Override
    public void register(String username, String password, String phone, String address) throws RequestParameterErrorException, UserExistException {
        if(ParameterUtil.isNull(username) || ParameterUtil.isNull(password) || ParameterUtil.isNull(phone) || ParameterUtil.isNull(address)){
            throw new RequestParameterErrorException("请求参数有误");
        }
        // 判断用户名是否已经被注册
        User selectUser = userDao.selectByUsername(username);
        if(selectUser != null){
            throw new UserExistException("该用户已经被注册");
        }

        // 注册用户,此时的密码需要加密
        User user = new User(username, MD5Util.md5(password),phone,address);
        userDao.insertUser(user);
    }

    @Override
    public void register(String username, String password, String phone, String address, FileItem file, HttpSession session) throws RequestParameterErrorException, UserExistException, FileUploadErrorException {
        if(ParameterUtil.isNull(username) || ParameterUtil.isNull(password) || ParameterUtil.isNull(phone) || ParameterUtil.isNull(address) || file.getSize() == 0){
            throw new RequestParameterErrorException("请求参数有误");
        }
        // 判断用户名是否已经被注册
        User selectUser = userDao.selectByUsername(username);
        if(selectUser != null){
            throw new UserExistException("该用户已经被注册");
        }

        String path = "/headImage/";
        String cp = session.getServletContext().getRealPath(path);
        // 注册用户,此时的密码需要加密
        User user = new User(username, MD5Util.md5(password),phone,address,path+file.getName());
        userDao.insertUser(user);

        // 上传头像
        File f = new File(path);
        try {
            f.mkdirs();
            file.write(new File(path,file.getName()));

        } catch (Exception e) {
            throw new FileUploadErrorException("头像上传失败");
        }
    }

    @Override
    public User login(String username, String password, String code, String image) throws RequestParameterErrorException, CodeErrorException, UserNotExistException {
        if(ParameterUtil.isNull(username) || ParameterUtil.isNull(password) || ParameterUtil.isNull(code) || ParameterUtil.isNull(image)){
            throw new RequestParameterErrorException("请求参数有误");
        }

        if(!code.equals(image)){
            throw new CodeErrorException("验证码不正确");
        }

        // 根据用户名与密码查询用户信息,密码需要加密
        User user = userDao.selectByUsernameAndPassword(username, MD5Util.md5(password));
        if(null == user){
            throw new UserNotExistException("用户名或密码错误");
        }

        return user;
    }
}
